我可以在不创建延期的情况下报告Q承诺进度吗?

时间:2013-12-13 12:20:40

标签: javascript promise q

我通过致电then来创建承诺 我可以以某种方式从内部报告进度,还是必须使用Q.defernotify)?

var promise = doSomething().then(function () {
   // somehow report progress from here
});

promise.progress(function (p) {
     console.log('progress', p);
});

3 个答案:

答案 0 :(得分:1)

使用deferred.notify()

自问这个问题以来已经有一段时间了,the Q library now support it

var progress = 96;
deferred.notify(progress);

例如:



function doSomething() {
    var deferred = Q.defer();
    
    setTimeout(function() {
        deferred.notify(10);
    },500);

    setTimeout(function() {
        deferred.notify(40);
    },1500);

    setTimeout(function() {
        deferred.notify(60);
    },2500);
    
    setTimeout(function() {
        deferred.notify(100);
        deferred.resolve();
    },3500);

    return deferred.promise;
}

doSomething()
    .then(
        function () {
            // Success
            console.log('done');
        },
        function (err) {
            // There was an error,
        },
        function (progress) {
            // We get notified of the progress as it is executed
            console.log(progress);
        });

<script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.4.1/q.js"></script>
&#13;
&#13;
&#13;

  

进度通知

     

承诺可以报告他们的进展,例如为了任务   这需要很长时间,如文件上传。不是所有的承诺都会   实施进度通知,但对于那些,你可以   使用第三个参数消耗进度值:

return uploadFile()
.then(function () {
    // Success uploading the file
}, function (err) {
    // There was an error, and we get the reason for error
}, function (progress) {
    // We get notified of the upload's progress as it is executed
});
     

与失败一样,Q也提供了一个名为进度回调的简写   进展:

return uploadFile().progress(function (progress) {
    // We get notified of the upload's progress
});

答案 1 :(得分:0)

我不完全确定“通过调用then创建承诺”的含义。我猜你的意思是你要用一个定义后的方式返回一个承诺?即,

var iAmAPromise = someOtherPromise.then(doSomething);

如果是这种情况,那么您可以使用适当的通知将doSomething包装在回调函数中。一个工作的例子:

var Q = require('q');

function resolver(deferred){
  return function(){
    deferred.resolve('return value from initial promise');
  }
}

function someOtherPromise( ms ) {
  var deferred = Q.defer();
  setTimeout( resolver(deferred) , ms );
  return deferred.promise;
}

function doSomething(data, cb){
  console.log('----- doing something with:', data);
  var val = "Did something with: " + data;
  cb(val);
}

function reportProgress(doSomething, notifierCb){
  notifierCb('waiting on it');
  return function(someOtherPromiseResponse){
    console.log('--- got promise response: ', someOtherPromiseResponse);
    notifierCb('got response',  someOtherPromiseResponse);
    console.log('--- do something with it');
    notifierCb('doing something with it');
    doSomething(someOtherPromiseResponse, function(val){
      notifierCb('done, result was:', val);
    });
  };
}

function notifier(update, detail){
  console.log('Notifier update:', update, detail||"");
}

function logError(err){
  console.log('ERROR:', err);
}

var iAmAPromise = someOtherPromise(1000).then(reportProgress(doSomething, notifier)).catch(logError);

console.log('  (Am I a Promise?)', Q.isPromise(iAmAPromise));

我可能误解了你的问题。

答案 2 :(得分:0)

原来:不,我不能。

另见it might not be a good idea after all