2013-12-13 27 views
3

我通過致電then來創建承諾。
我可以以某種方式報告其內部的進展,還是我必須使用Q.defer(其中有notify)?我可以報告Q承諾進展而不創建延期?

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

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

回答

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

雖然我可能誤解了你的問題。

+0

嗨,感謝您的回覆。我添加了一個代碼示例。我知道我可以使用任意回調,但是我想知道是否可以讓Q的'promise'處理程序在不使用'Q.defer'的情況下工作。 –

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>


進度通知

這是可能的承諾,報告他們的進步,例如對於需要很長時間才能上傳文件的任務 。並非所有的承諾將 執行進度的通知,但對於那些做,你可以 消耗使用第三個參數則進度值:

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