Given a function in a class that does processing on a text asynchronously. I also want my class to have a synchronous equivalent. Something like in the following example. Please help me how to achieve it.
class Processor {
processAsync(text) {
return new Promise((resolve, reject) => {
anotherPromise(this.text).then(() => {
resolve(processedText);
}).catch((err) => {
reject(new Error(`PROMISES FAILED`));
});
});
}
// THIS IS THE FUNCTION I WANT TO WRITE WHICH RETURNS THE VALUE OF THE PROCESSED TEXT
processSync(text) {
return Promise.resolve(this.processAsync(text));
}
}
function anotherPromise(text) {
return new Promise((resolve, reject) => {
if (someConditionWithText) {
return resolve
}
return reject(someerror);
});
}
Whenever I call processor.processSync('MyText')
it returns a promise, than the expected value. What must I do? I read that Promise.resolve() should do the work, but it does not.