is it possible to create a promise without using the constructor new promise() nor using =>resolve & =>rejected ? check following method
export function getItById(id) {
return fetch(`path`, {
method: 'GET',
}).then(rslt => {
if (!rslt || !rslt.ok) {
throw new Error('failure !');
} else {
return rslt.json();
}
});
}
I'm probably missing something, but it looks like everything is correct. The getItById function returns a promise. There is no need to create a promise.
So when using the function simply do
getItById(2).then(results => {
console.log(results)
})
To answer the question "is it possible to create a promise without using new promise" the answer is yes, every http call and every database call, or a file read or write returns a promise.