假设我有这个代码
function y(resolve, reject)
{
console.log("Result");
resolve();
}
var promise = new Promise(y);
Run Code Online (Sandbox Code Playgroud)
我想知道的是该函数是否y将异步执行.
鉴于客户端程序员既定义又使用resolve传递给Promise的执行器的函数,我天真地假设我完全控制了resolve函数的签名,因此我可以定义它接受多个参数。
然而以下代码(jsFiddle)失败了:
<html>
<script>
const promise = new Promise(function executor(resolve, reject) {
setTimeout(function () {
resolve(42, true);
}, 1000);
});
promise.then(function resolveCallback(answer, howCertain) {
console.log('callback called with arguments: ('+answer+', '+howCertain+')');
console.log('the answer is: '+answer+'. I am '+(howCertain?'very':'not very')+' certain about that');
});
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
上面的代码实际上打印在控制台上:
callback called with arguments: (42, undefined)
the answer is: 42. I am not very certain about that
Run Code Online (Sandbox Code Playgroud)
再深入一点,我写了下面的代码(jsFiddle):
<html>
<script>
const promise = new Promise(function …Run Code Online (Sandbox Code Playgroud)