I want a function that returns a message depending on the response from promises but I can't get the resolved value.
function create() {
var check = promise1();
var increment = promise2( check );
var create = promise3( increment );
if ( create === 1 ) {
return {
message: `Item with id ${increment} created`,
value: 'OK'
}
} else {
return {
message: `Item not created`,
value: 'FAIL'
}
}
}
var res = create()
// I want it to return the json if the item was created
// but instead they return promises and I have to use then
// I can't think of another way
console.log( res );
Would it help if I show you how the code from the promises look like?
The code is a lot more complex but I tried to simplify to the extreme.
//SOLVED EDIT:
function create( responsecb ) {
var check = promise1();
var increment = promise2( check );
var create = promise3( increment );
if ( create === 1 ) {
responsecb( {
message: `Item with id ${increment} created`,
value: 'OK'
} )
} else {
responsecb( {
message: `Item not created`,
value: 'FAIL'
} )
}
}
create.then(console.log)
I just have to create a callback which does something with the response.