Promises, pass additional parameters to then chain

后端 未结 5 1800
南旧
南旧 2020-11-28 21:10

A promise, just for example:

var P = new Promise(function (resolve, reject) {
  var a = 5;
  if (a) {
    setTimeout(function(){
      resolve(a);
    }, 3000         


        
5条回答
  •  天涯浪人
    2020-11-28 21:47

    Use currying.

    var P = new Promise(function (resolve, reject) {
        var a = 5;
        if (a) {
            setTimeout(function(){
                resolve(a);
            }, 3000);
        } else {
            reject(a);
        }
    });
    
    var curriedDoWork = function(text) {
        return function(data) {
            console.log(data + text);
        }
    };
    
    P.then(curriedDoWork('text'))
    .catch(
        //some error handling
    );
    

提交回复
热议问题