I have code like this.
async function doAsync(count){
//external function I need to use
count ;
console.log("async count is " count );
return await count;
}
function makeSyncChain(i){
//my chain that I could change
i=doAsync(i);
return i;
}
let val=0;
console.log("sync count is " makeSyncChain(val));
so like at the example, I have sync chain and one of the chain function is async, is there a way to make async function working in sync chain?
CodePudding user response:
Correct solution must be:
async function doAsync(count){
//external function I need to use
count ;
console.log("async count is " count );
return await count;
}
let val=0;
console.log("sync count is " (await doAsync(val)));
But in wrong key may be written as (and yes it won't work, it will just hang the browser):
async function doAsync(count){
//external function I need to use
count ;
console.log("async count is " count );
return await count;
}
function makeSyncChain(i){
//my chain that I could change
let isDone = false;
let result;
doAsync(i).then(r => {
result = r;
}).finally(() => {
isDone = true;
});
while (!isDone);
return result;
}
let val=0;
console.log("sync count is " makeSyncChain(val));