0

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.

TaoJS
  • 109
  • 1
  • 2
  • 13
  • 1
    Did you mean `promise3( increment );`? – thefourtheye Jan 22 '16 at 12:22
  • Yes just edited, thank you. – TaoJS Jan 22 '16 at 12:22
  • 2
    If you use promises inside `create()`, that makes it an asynchronous function. Every caller of `create()` then needs to treat it as an asynchronous function and use callbacks/promises to get its result. You can't synchronise an asynchronous function. – deceze Jan 22 '16 at 12:25
  • I think I got it, thank you for the already answered post, I will come back with a new version in no time as an answer. – TaoJS Jan 22 '16 at 12:31

0 Answers0