I am exploring the use of Promise.all(), but I don't know why it doesn't give me expect result. I try to illustrate it step by step.
Let take a look of my code:
var p2 = 1337;
var p3 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'foo');
});
var apiCall = async () =>{
// to simulate a api call that will response after 5 sec
setTimeout(() => {return 1000}, 5000);
}
Promise.all([p2,p3,apiCall()]).then(values => {
console.log(values); // [3, 1337, undefine], but I expect [3, 1337, 1000]
});
apiCall().then((response)=>{console.log(response)})
As my understanding, async function will immediately return a Promise, which is what Promise.all will wait for.
So I expect,
.then(values => {
console.log(values); // [3, 1337, undefined]
});
will only execute after 5 sec.
But the output is like below in 2 sec already, and not [3, 1337, 1000]
undefined
[ 1337, 'foo', undefined ]
I dont know where the problem lies, I expect
apiCall().then((response)=>{console.log(response)})
will give me "1000" instead of undefined
new edit
After gathering you guys answers, I tried this.
As my understanding, setTimeout is also a async, and it will automatically return a promise like any other promise.
So, based on this understanding, I write below code. but it doesnt work. I understand using Promise constructor will fix the problem. But I dont know what problem lies in this example
var apiCall = async () =>{
// to simulate a api call that will response after 5 sec
const a = setTimeout(() => {return 1000}, 5000);
return a
}