Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript promise回调参数来自哪里_Javascript - Fatal编程技术网

Javascript promise回调参数来自哪里

Javascript promise回调参数来自哪里,javascript,Javascript,我学习承诺。典型的承诺如下所示: function Promise(callback){ const handlers = []; function resolve(value){ for(const [success] of handlers) if(success) success(value); } function reject(error){ for(const [, catch] of handlers)

我学习承诺。典型的承诺如下所示:

 function Promise(callback){
   const handlers = [];

   function resolve(value){
     for(const [success] of handlers)
        if(success) success(value);
   }

  function reject(error){
     for(const [, catch] of handlers)
        if(catch) catch(error);
  }

  // Now call the callback and pass the functions:
  callback(resolve, reject);

  // Return the created promise...
}
让cleanMyRoomPromiseCallback=函数(resolve,reject){//“resolve”和“reject”来自哪里?
下定决心(“我的房间没人!”;
}
让cleanMyRoom=新承诺(cleanMyRoomPromiseCallback)
.然后((onCleanMyRoomResolve)=>{
console.log(onCleanMyRoomResolve);

})
是的,
Promise
构造函数将一个函数作为参数,它还需要两个函数作为参数(您在此处看到的
解析
拒绝
函数)。如果您想了解更多关于Promise构造函数的信息,MDN文章是一个很好的加速方式:


是的,
Promise
构造函数将函数作为参数,它还需要两个函数作为参数(您在此处看到的
resolve
reject
函数)。如果您想了解更多关于Promise构造函数的信息,MDN文章是一个很好的加速方式:


我将回答更广泛的问题“回调参数从何而来?”

简短而甜蜜的回答是,参数由调用回调的任何函数传递

一个简单的例子应该说明这一点

如果我们定义一个接受回调的函数:

function example(callback) {...}
回调函数需要在函数中的某个位置调用才能使用:

function example(callback) {
  callback('Hello World!')
}
当您想要使用该函数时,您需要知道将向回调传递哪些参数。在我们的示例中,只有一个参数,因此回调应该处理该参数:

example(function (text) {
  console.log(text) // writes "Hello World!" to the console
})


当您使用
newpromise
时,构造函数在调用其回调时传递两个函数参数。

我将回答一个更广泛的问题“回调参数从何而来?”

简短而甜蜜的回答是,参数由调用回调的任何函数传递

一个简单的例子应该说明这一点

如果我们定义一个接受回调的函数:

function example(callback) {...}
回调函数需要在函数中的某个位置调用才能使用:

function example(callback) {
  callback('Hello World!')
}
当您想要使用该函数时,您需要知道将向回调传递哪些参数。在我们的示例中,只有一个参数,因此回调应该处理该参数:

example(function (text) {
  console.log(text) // writes "Hello World!" to the console
})


使用
newpromise
时,构造函数在调用其回调时传递两个函数参数。

Promise构造函数的行为如下:

 function Promise(callback){
   const handlers = [];

   function resolve(value){
     for(const [success] of handlers)
        if(success) success(value);
   }

  function reject(error){
     for(const [, catch] of handlers)
        if(catch) catch(error);
  }

  // Now call the callback and pass the functions:
  callback(resolve, reject);

  // Return the created promise...
}

承诺构造函数的行为如下所示:

 function Promise(callback){
   const handlers = [];

   function resolve(value){
     for(const [success] of handlers)
        if(success) success(value);
   }

  function reject(error){
     for(const [, catch] of handlers)
        if(catch) catch(error);
  }

  // Now call the callback and pass the functions:
  callback(resolve, reject);

  // Return the created promise...
}

没错。
Promise
构造函数调用传递的回调函数并传递resolve和reject函数。这些函数的实际功能取决于浏览器的实现。是的,确实如此。
Promise
构造函数调用传递的回调函数并传递resolve和reject函数。这些函数的实际功能取决于浏览器的实现。