Моя цель — вернуть результат из асинхронной функции. (Или, другими словами, преобразовать асинхронную функцию в синхронную и вернуть из нее результат.)
Покажу это в коде:
Код: Выделить всё
function test1()
{
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Timer fires");
resolve("Done");
}
, 2000);
});
}
async function test2()
{
console.log("Before promise");
let result = await test1();
console.log("After promise");
console.log("result=" + result);
return result;
}
let final_result = test2();
console.log("Got final result=" + final_result);
Код: Выделить всё
Before promise
Timer fires
After promise
result=Done
Got final result=Done
Код: Выделить всё
Before promise
Got final result=[object Promise]
Timer fires
After promise
result=Done
Я явно что-то упускаю в потоке кода последней версии JS.
Подробнее здесь: https://stackoverflow.com/questions/793 ... ng-promise