Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建成功/错误的承诺,例如ajax_Javascript_Promise - Fatal编程技术网

Javascript 创建成功/错误的承诺,例如ajax

Javascript 创建成功/错误的承诺,例如ajax,javascript,promise,Javascript,Promise,我试图弄清楚如何使承诺在成功和错误回调中起作用,例如在ajax中 我有以下承诺: var promise = new Promise(function(success, error) { if (true) { success("Stuff worked!"); } else { error(Error("It broke")); } }); promise.then(function(resu

我试图弄清楚如何使承诺在成功和错误回调中起作用,例如在ajax中

我有以下承诺:

  var promise = new Promise(function(success, error) {
      if (true) {
         success("Stuff worked!");
      }
      else {
          error(Error("It broke"));
      }
    });
  promise.then(function(result)
  {
      console.log(result);
  }, function(err)
  {
      console.log(err);
  });
我希望它能像这样工作:

promise.success(function(response)
{
  console.log(response);
}).error(functin(err)
{
  console.log(err);
});

我该怎么办?

承诺只是使用不同的术语,你的
.success
.error
翻译成
。然后
抓住承诺

promise
    .then(function(value) {
        console.log(value);
    })
    .catch(function(err) {
        console.log(err);
    });
如果你真的想(请不要),你可以这样做

Promise.prototype.success = Promise.prototype.success || Promise.prototype.then;
Promise.prototype.error = Promise.prototype.error || Promise.prototype.catch;

success
error
只是语法上的糖分,其作用与以下相同:

function CustomPromise(callback){
  this.promise = new Promise(callback);
}

// Success and error are merely shorthand functions 
CustomPromise.prototype.success = function(callback){
  this.promise.then(callback,null);
};

CustomPromise.prototype.error = function(callback){
  this.promise.then(null,callback);
}

// Expose the rest of the promise interface, especially `then` and `catch`
请注意,我没有扩展本机
Promise
原型,因为这是一种糟糕的做法


我建议您坚持使用
然后(成功,错误)
,因为这已经成为承诺的“标准”界面。jQuery和其他几个LIB在某种程度上遵循这一点。

那么$.post是如何做到的呢?有什么想法吗?@DalaiLama jQuery使用了另一种(坏的)承诺,不符合ECMAScript或承诺a+规范。我不会把它作为一个例子(或者就此使用它)。以$HTTPPOST为例。我仍然需要测试它,为此我需要模仿它的行为。“我不会在我的代码中使用它,我要测试它。”达赖喇嘛,然后使用它。至于它如何定义它们,它所做的只是
req.success=function(fn){return req.then(function(res){return res.data;}).then(fn);}
之类的东西。@BenjaminGruenbaum我知道如果我添加了标签,你会来救我的!:数据就是这样,但是记住使用
返回这个在成功/错误函数之后,这样你可以链接它们。本机承诺不是子类-这将在不久的将来在浏览器中出现,因为它们不能很好地实现规范-还请注意,你应该
.prototype=Object.create(prototype.prototype)
并调用
承诺(this)
-再次声明:这是所有规范要求本机承诺应该做但不应该做的事情。此外,我真的不相信扩展承诺的原生原型是一个如此糟糕的想法,但这是主观的。