【发布时间】:2021-01-25 18:36:49
【问题描述】:
如何在getValue1函数中实现回调?
现在我变得不确定了?
我需要在 getAll 函数中获取“B”值
getAll 函数需要返回一个 promise,并且 getValue1 函数不能被更改。我该怎么做?
function getValue1(callback) {
setTimeout(() => {
callback("B");
}, 10);
}
function getAll() {
return Promise.all([getValue1(b => b)]);
}
getAll().then((arr) => console.log(arr));
【问题讨论】:
-
这是奇怪的代码......为什么你使用 Promise.all 来处理不使用承诺的代码?如果您想在
Promise.all中轻松使用它,您需要从getValue1返回一个Promise,而不是使用回调参数。 -
getAll 需要返回一个 promise,并且 getValue1 函数不能更改。我该怎么做?
-
function getValue1 无法更改,如何实现回调?
标签: javascript node.js ecmascript-6 promise