I have some bussiness logic function to call , which has a logic that must use HttpGet , And I have to wait until it returns the result to contiune ,if i use jquery's ajax can simple do it all , do not know whether the Observable also has similar way?
I hope the resut is :
- John
- Andy
but now the result is only show Andy :(
function main(){
/*
I have more than 70 sharing rules to deal with different Logic
(e.g. getAge , getSomthing...), in order to simplify the problem ,
I only list two rules as a demonstration
*/
methods = [
getNameFromServer,
getSomeOneName
];
const result = [];
methods.forEach(method => {
method(result);
})
console.log(result);
}
function getNameFromServer(result){
Rx.Observable.of('John')
.delay(1000)
.subscribe(name => {
console.log('now async get name , but I need it will be sync')
result.push(name)
});
// can I use sync Ajax like jquery's code?
// $.ajax({
// ... ,
// async: false
// })
// .done(response => result.push(response.xxx))
}
function getSomeOneName(result){
result.push('Andy');
}
// execute
main();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.1/Rx.js"></script>
</head>
<body>
</body>
</html>