相关疑难解决方法(0)

JavaScript Promise Callback是否异步执行

假设我有这个代码

function y(resolve, reject)
{
  console.log("Result");
  resolve();
}  

var promise = new Promise(y);
Run Code Online (Sandbox Code Playgroud)

我想知道的是该函数是否y将异步执行.

javascript promise es6-promise

14
推荐指数
2
解决办法
2515
查看次数

Promise 执行器中的 `resolve` 和 `reject` 函数

鉴于客户端程序员既定义又使用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)

javascript

4
推荐指数
1
解决办法
705
查看次数

标签 统计

javascript ×2

es6-promise ×1

promise ×1