2016-06-13 45 views
-1

所以我有這樣的代碼:如何防止藍鳥的警告:承諾在處理程序創建的,但不是從它返回

profileRepository.get(profileUuid).then((profile) => { 
    if (profile.fileUuid) { 
    profileStore.getFile(profile.fileUuid).then((fileData) => { 
     data.fileData = fileData; 
    }, callback); 
    } 

    data.profile = profile; 
}, callback); 

和我收到警告:

Warning: a promise was created in a handler but was not returned from it 

哪我相信正在發生,因爲profileStore.getFile()也回報了諾言。現在總體地擺脫警告的方式是鏈then()的,做更多的東西一樣:

profileRepository.get(profileUuid).then((profile) => { 
    if (profile.fileUuid) { 
    return profileStore.getFile(profile.fileUuid); 
    } 

    data.profile = profile; 
}, callback) 
.then((fileData) => { 
    data.fileData = fileData; 
}, callback); 

的問題是,我要叫profileStore.getFile()條件,所以我不看我怎麼能然後在這種情況下使用鏈接,也不會看到以任何其他方式重新編寫此代碼以防止發生警告的方法。我試圖從get()成功回調中嘗試return null,但這甚至不能防止警告。

警告只是導致我的控制檯日誌填滿這使得它真的很煩人,當我需要做調試和東西,確實的方法哪位知道,以防止這個用例這樣的警告?

UPDATE

我也有同樣的警告嘗試這樣做:

profileRepository.get(profileUuid).then((profile) => { 
    if (profile.fileUuid) { 
    return profileStore.getFile(profile.fileUuid); 
    } 

    data.profile = profile; 
}, callback) 
.then((fileData) => { 
    data.fileData = fileData; 
}, callback); 
+1

簡單,恢復從那裏告訴你需要 –

+2

您的使用承諾'callback'看起來很奇怪。 – robertklep

+0

@JarrodRoberson,我試着用相同的警告(更新後)。 – ryanzec

回答

0

的問題是,我必須調用profileStore.getFile()條件,所以我不看如何在此情況下使用鏈接

您不需要在此處使用鏈接。但是如果您在then回調中執行異步操作,您總是需要承諾return,否則外部承諾不會等待它(並且您不希望這樣做)。

在你的情況,你可以從回調返回一個承諾白白讓這裏等到data.fileData我被分配:

return profileRepository.get(profileUuid).then(profile => { 
    data.profile = profile; 
    if (profile.fileUuid) 
    return profileStore.getFile(profile.fileUuid).then(fileData => { 
     data.fileData = fileData; 
    }); 
}).then(() => data, callback); 
相關問題