gpt4 book ai didi

javascript - Promise 和 Node 使变量在不同的函数中可用

转载 作者:行者123 更新时间:2023-12-03 04:06:47 27 4
gpt4 key购买 nike

我有三个函数,我希望函数一和函数二中的变量在函数三中可用。

功能一

在函数一中,我尝试包含该变量 emailUser 来解析以在第三个函数中使用它。

var firstMethod = function() {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
app.post('/api/data', function (req, res) {
console.log(req.body);
var emailUser = req.body.email;
res.send(emailUser);
});
console.log('first method completed');
resolve({data: emailUser });
}, 2000);
});
return promise;
};

第二个函数

我试图传递此函数 api_key 以在第三个函数中使用。

var secondMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
nodePardot.PardotAPI({
userKey: 34535345,
email: fsf@dd.com,
password: fd3sv34f,
DEBUG: true
}, function (err, client) {
if (err) {
// Authentication failed
console.error("Authentication Failed", err)
} else {
// Authentication successful
var api_key = client.apiKey;
console.log("Authentication successful !", api_key);
}
});
console.log('second method completed');
resolve({newData: api_key});
}, 2000);
});
return promise;
};

第三个函数

这个函数我想访问函数一和函数二中的变量。我已经包含了一个控制台日志,以便我可以看到控制台上打印的变量。

我还想访问这些函数以在第三个函数/方法中使用。

  var thirdMethod= function() {
var promise = new Promise(function(resolve, reject){
console.log('show both functions', emailUser, api_key);
}, 3000);
});
return promise;
};

firstMethod()
.then(secondMethod)
.then(thirdMethod);

最佳答案

当 API 调用得到响应时,您需要在函数体内进行解析。

var firstMethod = function() {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
app.post('/api/data', function (req, res) {
console.log(req.body);
var emailUser = req.body.email;
res.send(emailUser);

//resolve when get the response
resolve({data: emailUser });
});

}, 2000);
});
return promise;
};
<小时/>

出现错误时您必须需要解决或拒绝。这里我解决了错误并将 api_key 设置为 empty

var secondMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
nodePardot.PardotAPI({
userKey: 34535345,
email: fsf@dd.com,
password: fd3sv34f,
DEBUG: true
}, function (err, client) {
if (err) {
// Authentication failed
console.error("Authentication Failed", err);
resolve({newData: ''});

} else {
// Authentication successful
var api_key = client.apiKey;

console.log("Authentication successful !", api_key);
resolve({newData: api_key});
}
});

}, 2000);
});
return promise;
};
<小时/>
function thirdMethod(result) {
console.log('show both functions', result[0].data, result[1].newData);
};

Promise.all([firstMethod(), secondMethod()])
.then(thirdMethod);
<小时/>

供引用

var firstMethod = function() {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {

//resolve when get the response
resolve({
data: "a"
});
});

}, 2000);

return promise;
};

var secondMethod = function() {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {

//resolve when get the response
resolve({
data: "b"
});
});

}, 2000);

return promise;
};

var thirdMethod = function(result) {
console.log(result[0].data, result[1].data);
};

Promise.all([firstMethod(), secondMethod()]).then(thirdMethod);

输出:a b

关于javascript - Promise 和 Node 使变量在不同的函数中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44516583/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com