[英]Javascript: Return promise in nested function
我有這個代碼:
createOrUpdate({
test: 'test'
}).then((response) => {
console.log(response);
});
期望createOrUpdate返回一個promise。 這是createOrUpdate函數:
function createOrUpdate(requestParams) {
var options = {};
return getService('global', function(error, serviceResult) {
if (error) {
log.error(error);
return callback(error, null);
}
//...
return requestPromise(options);
});
}
我有這個getService方法,需要一些參數和一個回調。 我需要獲取return requestPromise(options)
以在調用createOrUpdate時返回,但是當前它不起作用。 我收到一個錯誤,說我無法調用createOrUpdate返回的內容。
想法?
很簡單,您需要將getService
變為promise返回API 。
否則,節點回調函數不會return
任何內容並忽略它們自己的返回值。
function createOrUpdate(requestParams) {
var options = {};
return fromCallback(getService)('global').then(options =>
requestPromise(options);
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.