最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is then not working on this function - Stack Overflow

programmeradmin7浏览0评论

I tried testing the example given in MDN in one of the promise implementation. Its giving me the error as below.

Error

Uncaught TypeError: doSomething(...).then is not a function
    at promise.js:16

JS file

function successCallback(result) {
    console.log("Success" + result);
}

function failCallback(fail) {
    console.log('fail' + fail);
}

function doSomething(successCallback, failCallback) {
    return "Yello";
};

doSomething(successCallback, failCallback);


doSomething().then(successCallback,failCallback);

I tried testing the example given in MDN in one of the promise implementation. Its giving me the error as below.

Error

Uncaught TypeError: doSomething(...).then is not a function
    at promise.js:16

JS file

function successCallback(result) {
    console.log("Success" + result);
}

function failCallback(fail) {
    console.log('fail' + fail);
}

function doSomething(successCallback, failCallback) {
    return "Yello";
};

doSomething(successCallback, failCallback);


doSomething().then(successCallback,failCallback);
Share Improve this question asked Jun 24, 2017 at 9:59 user3310115user3310115 1,4604 gold badges26 silver badges59 bronze badges 4
  • There's not promise to be seen in your code. – robertklep Commented Jun 24, 2017 at 10:01
  • You defined a function called doSomething, not a promise – Omri Luzon Commented Jun 24, 2017 at 10:01
  • then what is given in this url? developer.mozilla/en-US/docs/Web/JavaScript/Guide/… – user3310115 Commented Jun 24, 2017 at 10:03
  • 2 The example they show assumes a function that returns a promise, and they contrast it with a function that accepts callbacks. There's nothing magic about promises or functions that return them-it's normal JavaScript, and you still need an object to call "then" on. – Dave Newton Commented Jun 24, 2017 at 10:34
Add a ment  | 

5 Answers 5

Reset to default 6

Even easier with async :

async function doSomething() {
  return "Yello";
};

doSomething().then(console.log);//Yello

To enable error handling, simply throw an error:

async function isMultiple(a,b) {
   if(a%b===0){
      return true;//success
   }
      throw "not a multiple";
};

isMultiple(5,2).then(console.log).catch(console.log);//not a multiple in catch

Note that async functions are part of ES7 ( very very new )...

babeled code

You'll have to return a Promise which resolves in the case of success or rejects in case of error to the then.

function successCallback(result) {
  console.log("Success" + result);
}

function failCallback(fail) {
  console.log('fail' + fail);
}


function doSomething(successCallback, failCallback) {
  return new Promise( (resolve, reject) => {
      resolve('Yello!');
      // or
      // reject ("Error!");
  });
}

doSomething().then(successCallback, failCallback);

In your example, you are not using a Promise. To use one, you must do :

function successCallback(result) {
    console.log("Success" + result);
}

function failCallback(fail) {
    console.log('fail' + fail);
}

function doSomething() {
    return new Promise(function(resolve,reject) {
        console.log("Yello");
        resolve();
    });
}


doSomething().then(successCallback,failCallback);

Here, the failCallback will never be executed

Your doSomething function has to return a Promise. For Example:

return Promise.resolve('Hello');

doSomething Object does not have then function , doSomething is just a function

发布评论

评论列表(0)

  1. 暂无评论