简体   繁体   English

在另一个异步函数中从诺言解析中获取结果

[英]Get result from promise resolve in another async function

I'm trying to get the promise resolve result when it ends the Promise in the first function (topromise). 当它在第一个函数(topromise)中结束Promise时,我试图获得Promise解决结果。 Therefore as you'll see below I'm creating another Promise.resolve(pageData) with the value from the last then to try to get the value in my getpromise function. 因此,正如您将在下面看到的那样,我将使用上一个值创建另一个Promise.resolve(pageData),然后尝试在我的getpromise函数中获取该值。

This is my code: 这是我的代码:

 function topromise(param){ let pageData; new Promise((resolve, reject)=>{ resolve(param) }) .then((value)=>{ console.log(value) return "hola" }) .then((value)=>{ console.log(value) pageData= "bon jour" return getpromise(Promise.resolve(pageData)) }) } topromise("hello") function getpromise(value){ .then(value=> console.log(value)) //I want to get the pageData result from resolve } 

Your topromise() function is missing a return , and your getpromise function seems to begin in the middle of a method call. 您的topromise()函数缺少return ,而您的getpromise函数似乎始于方法调用的中间。

It's very unclear what you're trying to do, but maybe you're going for something like this. 目前尚不清楚您要做什么,但也许您正在寻求类似的东西。 This is working code: 这是工作代码:

 function topromise(param){ let pageData; return Promise.resolve(param) .then((value)=>{ console.log(value) return "hola" }) .then((value)=>{ console.log(value) pageData = "bon jour" return pageData; }) } getpromise(topromise("hello")) function getpromise(value){ value .then(result => console.log(result)) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM