2017-05-09 34 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() 
 
    
 
    
 
    } 
 

 
}

相关问题