Reputation: 6963
I'm trying to create a JavaScript/typescript function that wraps a promise, by returning a new Promise, adding a try catch and a call back for user to write the code. This is the concept:
function XPromise(code) {
return new Promise((resolve, reject) => {
try {
resolve(code());
} catch (exception) {
reject(exception);
}
});
}
But how would I use the snippet above in something like this:
async function GetData(testClient, project, testPlan, suiteId) {
return XPromise(code => {
console.debug("GetData");
testClient.getData(project, testPlan, suiteId)
.then(data => {
if (data.length === 0) reject(data);
resolve(data);
});
}
The legacy code uses the .then construct which is ideal place to put reject and resolve, but no function exists to do that.
If I do this:
function XPromise(code, resolve, reject) {
return new Promise((resolve, reject) => {
try {
resolve(code());
} catch (exception) {
reject(exception);
}
});
}
async function GetData(testClient, project, testPlan, suiteId) {
return XPromise(code => {
console.debug("GetData");
testClient.getData(project, testPlan, suiteId)
.then(data => {
if (data.length === 0) reject(data);
resolve(data);
});
},
resolve => { },
reject => { }
}
I don't know how to get the then logic to do it's thing in the "lower" functions.
The reason I want the new Promise wrapper is that I have tons of these things to implement... I don't want to do this throughout the code. This code compiles, but requires me to write New Promises and Try Catch statements for each function I implement.
async function GetData(testClient, project, testPlan, suiteId) {
return new Promise((resolve, reject) => {
console.debug("GetPoints");
try {
testClient.getData(project, testPlan, suiteId)
.then(data => {
if (data.length === 0) reject(data);
resolve(data);
});
} catch (exception) { reject(exception); }
});
}
Upvotes: 3
Views: 3046
Reputation: 506
you should not pass value params resolve,reject to new Promise(). They are given by native promise object while invoking your callback . you just have to invoke it to make that promise either resolve or reject.
new Promise((resolve,reject)=>{
if(condition){
resolve();// resolve is callback sent by promise object. you just invoke it
}else{
reject(); like resolve reject makes this promise to fail
}
});
i think your last mentioned logic is correct
async function GetData(testClient, project, testPlan, suiteId) {
return new Promise((resolve, reject) => {
console.debug("GetPoints");
try {
testClient.getData(project, testPlan, suiteId)
.then(data => {
if (data.length === 0) reject(data);
resolve(data);
});
} catch (exception) { reject(exception); }
});
}
note : async/await is not a replacement for promise . if you want to create a promise sure , you have to create it. you can use async/await to only avoid then chains and your code looks like synchronous and easy to understand. but remain they are still asynchronous.
but in your case you can use Promise.resolve() , Promise.reject() to avoid new Promise(). see below,
async function GetData(testClient, project, testPlan, suiteId) {
console.debug("GetPoints");
try {
const data = await testClient.getData(project, testPlan, suiteId);
if (data.length === 0) return Promise.reject(data);
return Promise.resolve(data);
} catch (exception) { return Promise.reject(exception); }
}
Upvotes: 5