2017-05-09 35 views
0

嗨我試圖通過執行一個屬於另一個模塊(另一個JavaScript文件)的函數來解析已經解析的Promise,以便通過.then得到值。 。Nodejs將promise解析傳遞給另一個模塊以獲取值

這是我的代碼:

saveUrl.js文件:

const manager = require('./manager'); 
 

 
.then(()=>{ 
 
return new Promise((resolve, rej)=>{ 
 
\t resolve(mainUrl) 
 

 
     //here if I put .then I have the value that I want BUT I want the value to be passed to the manager file 
 

 
     manager.getSavedUrlPromise() 
 
    }) 
 
})

法力ger.js文件:

const mongoSavedUrl = require('./saveUrl'); 
 

 

 
module.exports={ 
 
    
 
    getSavedUrlPromise(){ 
 
    return mongoSavedUrl 
 
    .then(()=>{ 
 
     console.log("inside getSavedUrlPromise") 
 
     console.log(promise) 
 
     process.exit() 
 
    }) 
 
    
 
    
 
    
 
    } 
 
    
 

 
}

它告訴我說:

mongoSavedUrl.then is not a function

+0

'決心(mainUrl)' - 這是什麼 「決心」,沒有承諾明顯的在那裏做什麼?另外,saveUrl.js沒有導出,也沒有任何東西返回任何地方 –

回答

0

現在好了,它的完成,這是工作流程的錯誤。

在saveUrl文件,它應該是以下幾點:

.then((res) => { 
 
\t \t resolve(mainUrl); 
 
}); 
 

 
// I handle the resolve here and decided to call the module function afterwards 
 
.then((url) => { 
 
    return manager.getSavedUrlPromise(url) 
 
});

而且在我的經理文件我並不需要把所有。然後,因爲承諾在已經處理該saveUrl文件,因爲我會從那裏傳值:

module.exports={ 
 
    
 
    getSavedUrlPromise(url){ 
 
    
 
     console.log("inside getSavedUrlPromise") 
 
     console.log(url) 
 
     process.exit() 
 
    
 
    
 
    } 
 

 
}

相關問題