0

Is there a way I can achieve this where toys is a deferred API request and when run the value of res is returned and is the end result for when toysData is logged.

the promise

const toys = (url) => {
  const deferred = $q.defer();
  const params = {
    id: url,
  };

  ToysModel.get(params, (data) => {
    deferred.resolve(data);
  }, (error) => {
    deferred.reject(error);
  });
  return deferred.promise;
};

the request

let toysData = toys(uuid)
    .then(res => res);

console.log(toysData);

api call

export default class ToysModel {
  constructor($resource, ENV) {
    const url = `${ENV.apiEndpoint}/`;

    const actions = {
      oembed: {
        url: `${ENV.apiEndpoint}/toy/:id`,
        method: 'get',
        params: {
          id: '@id',
        },
        crossDomain: true,
      },
    };
    return $resource(url, {}, actions);
  }
}

1 Answers1

0

that variable toysData would have promise object. It mens that you can subscribe to success or fail state. If you want to log response you just need to subscribe on success state with log function

toys(uuid).then(console.log);
Andy
  • 523
  • 2
  • 13