Helper and returning the resolved value of a promise

I have this Helper:

export function myHelper(params) {
  let myObject = params[0];
  return myObject.myMethod();
}

But this is sending this error to console:

my-helper.js:8 Uncaught TypeError: myObject.myMethod is not a function

I think it is because myObject is an unresolved promise.

I have tried this:

return myObject.then((myObject) => { return myObject.myMethod(); });

But it is not working.

How can I return the resolved value of a promise?

1 Like

You need to remove the comments: (params */, hash */) should be (params)… Here’s the link to documentation for helpers: Writing Helpers - Templates - Ember Guides . good luck

I have removed the comment in my code example. Don’t think this is relevant though :slight_smile:

To return the resolved value of a promise, you need to use the then(), like you did:

return promise.then( value => { return value } )

I see a naming collision on “myObject” that is assigned both on the params[0] value (the promise) and the argument of the then call (the promised value) you should rename that

export function myHelper(params) {

    var promise = params[0]
    return promise.then( ( myObject ) => {
        return myObject.myMethod()
    }  )
}

but in case that is not your problem, try checking with debugger/console.log the first/most stupid things that could go wrong:

  • is params an array?
  • is params[ 0 ] a promise?
  • is myObject an object?
  • is myObject.myMethod a function?
  • does myObject.myMethod return something? etc.

that’s what I do when I am in these situation, based on my desperation level:

export function myHelper(params) {

    console.log( "params: ", typeof( params ) ) 
    var promise = params[0]
    console.log( "promise: ", typeof( promise ) ) 
    debugger;

    return promise.then( ( myObject ) => {

        console.log( "myObject: ", typeof( myObject ) ) 
        console.log( "myMethod: ", typeof( myObject.myMethod ) ) 
        let value = myObject.myMethod()
        console.log( "value: ", typeof( myObject.myMethod( ) ) ) 
        debugger;

        return value
    }  ).catch( error => { 
        console.log( "got error:", error )
        debugger ;
    } )
}

and so on… hope that helped :slight_smile:

1 Like