2016-05-23 33 views
2

我在我的應用程序中使用bluebird,並使用babel將我的代碼編譯爲es5。然而,我總是得到這個警告,並且我已經檢查過,每件Promise都有return的價值。承諾是在處理程序中創建的,但並未從藍鳥中返回

這裏是我的代碼:錯誤信息

Promise.promisifyAll(fs); 

use.login().then((result) => { 
    console.log(result); 
    doSomething('../test.png'); 
}); 

function doSomething(filepath) { 
    return fs.readFileAsync(filepath).then((bufs) => (
     doPost(url, bufs, filepath) 
     .then((res) => (
      res.error ? Promise.reject(res.error) : Promise.resolve(res)) 
     ) 
)).catch((err) => { 
    console.error(err); 
    return err; 
    }); 
} 


function doPost(url, bufs = null, filepath = null) { 
    return new Promise((resolve, reject) => (
    unirest.post(url) 
     .headers(config.Headers) 
     .timeout(120000) 
     .field(bufs) 
     .attach('files', filepath) 
     .end((res) => (
     res.error ? reject(res.error) : resolve(res) 
    )) 
)); 
} 

詳情:

Warning: a promise was created in a handler but was not returned from it 
at doSomething (/home/test/Documents/test/lib/abc.js:2:27) 
// This line number is referred to the compiled code which is equal to line 4:12 in the above code 
+0

你在哪裏調用'doSomething'?那是你的整個代碼嗎? – Bergi

+0

@Bergi不,那不是我的整個代碼,但我只在上面的代碼中得到了警告。當我需要讀取文件時,我會調用'doSomething',例如。 'doSomething('../ test.png')' – CYB

+0

我的意思是說,也許你是從一個承諾處理程序調用'doSomething',它不會返回任何東西。這是警告的整個調用堆棧嗎?如果你能顯示整個代碼將會很有幫助。 – Bergi

回答

0

感謝@Bergi,我沒有查諾堆在那裏我打電話doSomething所以我修改後的報警消失我的代碼如下

use.login().then((result) => { 
    console.log(result); 
    doSomething('../test.png'); 
    return null; // Add this line to make promise return anything, then the warning gone 
});