gpt4 book ai didi

javascript - Node 类型错误 : Promise resolver # is not a function

转载 作者:行者123 更新时间:2023-11-30 20:12:27 25 4
gpt4 key购买 nike

我在第一次拒绝时尝试使用的 promise 出现类型错误,但据我所知,它看起来像我找到的示例。我做错了什么?

var resolve, reject;
exports.handler = (event, context, callback) =>
{

//var filePromise = new Promise(ProcessFile(resolve, reject));



var filePromise = new Promise(function ProcessFile()
{
console.log("Processing file");
return new Promise((resolve, reject) => {
var array;

if(array.length <0)
{
resolve("We have stuff in the array, looks promising");
}
else
{
reject("Something went wrong populating the array");
}

});
});

filePromise.then(CheckHeaders());

function CheckHeaders()
{
return new Promise((resolve, reject) =>
{
console.log("Checking headers");

reject("Unauthorised");
}); //close promise
} //close function
};

最佳答案

你只需要一个 promise 。

var filePromise = new Promise(function(resolve, reject) {
console.log("Processing file");
var array = [1];

if (array.length > 0) {
resolve("We have stuff in the array, looks promising");
} else {
reject("Something went wrong populating the array");
}
});

function CheckHeaders() {
return new Promise((resolve, reject) => {
console.log("Checking headers");

reject("Unauthorised");
}); //close promise
}

filePromise
.then(res => {
console.log(res)
CheckHeaders()
.then(res2 => console.log(res2))
}).catch(err => console.warn(err));

正如@Bergi 指出的那样,最好只有一个整体 promise 。

var filePromise = new Promise(function(resolve, reject) {
console.log("Processing file");
var array = [1];

if (array.length > 0) {
resolve("We have stuff in the array, looks promising");
} else {
reject("Something went wrong populating the array");
}

//here check headers
reject("Unauthorised");


});

filePromise
.then(res => {
console.log(res)
}).catch(err => console.warn(err));

关于javascript - Node 类型错误 : Promise resolver #<Promise> is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52257160/

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