javascriptasynchronouscallbackpromisealchemyapi

Promise.promisify is not a function


I wrote JavaScript like this:

var keys=null;
var promise=Promise.promisify(alchemyapi.keywords("url",myUrl,{},function(response) {
    var keywords = { url:myUrl, response:JSON.stringify(response,null,4), results:response['keywords'] };
                return keywords;
            }));
promise.then(
                (result)=>{
                    var keys=result;
                    console.log(keys);
                },
                (error)=>console.log(error)
            );

I'm using AlchemyAPI and trying to store data I got into my database How should I do?


Solution

  • You should be able to use Promise to return expected results by removing .promisify which is not a built-in Promise method ; substituting passing keywords to resolve within Promise constructor for return

    var keys = null
    , promise = new Promise(function(resolve) {
        alchemyapi.keywords("url", myUrl, {}, function(response) {
          var keywords = {url: myUrl
                          , response: JSON.stringify(response,null,4)
                          , results:response['keywords'] 
                         };
          resolve(keywords);
          // error handling ?
        })
      }).then(function(result) {
          keys = result;
          console.log(keys)
      }, function(err) {
          console.log(err)
      })