0

I have a function that does the following:

function getUrl(id) {
    let params;
    return attModel.getById(id)
      .then(resData => {
        const attachmentMetaData = resData.shift();
        params = {
           Bucket: 'some-bucket',
           Key: [
             attachmentMetaData.fieldA,
             attachmentMetaData.fieldB,
             attachmentMetaData.fieldC
           ]
        };
        return isEnabled(attachmentMetaData.fieldD);
  })
  .then(isEnabled => {
    if (isEnabled) params.Key[1] = '0';
    params.Key = `${params.Key.join('/')}.png`;
    return params;
  })
  .then(params => { return s3.getSignedUrl('getObject', params); });
}

When I call this function from the controller, and console.log the result, I get the following:

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined,
  _trace: 'some traces ...'
}

I've tried attaching a .then as suggested by Promise fulfillment handler undefined, but still get the same result.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
uckc
  • 21
  • 3
  • 1
    You should not care about the internal representation of the promise - everything that begins with an underscore. Hint: the final result promise always has no handlers. – Bergi May 25 '17 at 04:41
  • You don't want to log the promise, you want to chain `.then(console.log, console.error)` – Bergi May 25 '17 at 04:42
  • To clarify, I'm trying to set the result of getUrl(1) to a variable and use it. But when I console.log to check the result, I am expecting a URL in string, but I get a Promise instead. I tried chaining a .then(url => { return url; }); but that also didn't work. @Bergi – uckc May 25 '17 at 04:46
  • 1
    You cannot assign a future result to a variable right now. Store the promise, and when you need the result wait for it by adding a callback and using the value inside there. – Bergi May 25 '17 at 04:50
  • @uckc `.then()` always returns a promise. It will never return anything else. You need to take Bergi's advice. – JLRishe May 25 '17 at 05:39

0 Answers0