Reputation: 49
I'm getting a type error on the promise I'm trying to use around the first reject, but from what I can tell it looks like the examples I've found. What am I doing wrong?
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
};
Upvotes: 2
Views: 7655
Reputation: 17606
You only need one 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));
As @Bergi pointed out it would be better to only have one overall 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));
Upvotes: 2