【问题标题】:Can I report Q promise progress without creating a deferred?我可以在不创建延迟的情况下报告 Q 承诺进度吗?
【发布时间】:2016-07-18 15:07:37
【问题描述】:

我正在通过调用 then 创建一个承诺。
我可以以某种方式从其中报告进度,还是必须使用Q.defer(有notify)?

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

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

【问题讨论】:

    标签: javascript promise q


    【解决方案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>

    进度通知

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

    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
    });
    

    和 fail 一样,Q 也提供了进度回调的简写,称为 进展:

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

    【讨论】:

      【解决方案2】:

      我不确定您所说的“通过调用 then 创建承诺”是什么意思。我猜你的意思是你正在返回一个带有 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));
      

      我可能误解了你的问题。

      【讨论】:

      • 您好,感谢您的回复。我添加了一个代码示例。我知道我可以使用任意回调,但我想知道是否可以在不使用 Q.defer 的情况下以某种方式使 Q 的 promise 处理程序工作。
      【解决方案3】:

      事实证明:不,我不能。

      了解为什么it might not be a good idea after all

      【讨论】:

        猜你喜欢
        • 2013-09-11
        • 2017-05-08
        • 2018-10-15
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 2015-03-08
        • 2017-06-25
        • 1970-01-01
        相关资源
        最近更新 更多