Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 用于承诺链的Polyfill_Javascript_Asynchronous_Promise_Es6 Promise_Polyfills - Fatal编程技术网

Javascript 用于承诺链的Polyfill

Javascript 用于承诺链的Polyfill,javascript,asynchronous,promise,es6-promise,polyfills,Javascript,Asynchronous,Promise,Es6 Promise,Polyfills,试图找出polyfill的承诺错误。当链接第二个时,程序无法按预期工作。有人知道问题出在哪里吗 function PromisePolyfill(executor){ const PENDING = 1; const FULFILLED = 2; const REJECTED = 3; let state = PENDING; let value = null; let handlers = []; let catches = []; function resolve(result){

试图找出polyfill的承诺错误。当链接第二个时,程序无法按预期工作。有人知道问题出在哪里吗

function PromisePolyfill(executor){
const PENDING = 1;
const FULFILLED = 2;
const REJECTED = 3;

let state = PENDING;
let value = null;
let handlers = [];
let catches = [];

function resolve(result){
    if( state !== PENDING) return;
    value = result;
    state = FULFILLED;
    handlers.forEach((h)=> h(value));
    return this;
}

function reject(err){
    if(state !== PENDING) return;
    value = err;
    state = REJECTED;
    catches.forEach((h)=>h(value));
}

this.then = function(callback){
    if(state === FULFILLED){
       callback(value);
       
    } else {
        handlers.push(callback);
    }
    return this;
}

executor(resolve, reject);

}

var func = (resolve,reject)=> {
     setTimeout(()=>{
        resolve('Hey I am resolved');
     },3000)
  }

 var basicPromise = new PromisePolyfill(func);
  basicPromise.then( (res) => {
    console.log(res);
   return new PromisePolyfill(func);
   } )
   .then((res)=> {
   console.log(res);
   })
尝试调试自己的promise polyfill,但在发生错误时失败,在这一点上停留了一段时间,需要专家找出问题所在吗

function PromisePolyfill(executor){
const PENDING = 1;
const FULFILLED = 2;
const REJECTED = 3;

let state = PENDING;
let value = null;
let handlers = [];
let catches = [];

function resolve(result){
    if( state !== PENDING) return;
    value = result;
    state = FULFILLED;
    handlers.forEach((h)=> h(value));
    return this;
}

function reject(err){
    if(state !== PENDING) return;
    value = err;
    state = REJECTED;
    catches.forEach((h)=>h(value));
}

this.then = function(callback){
    if(state === FULFILLED){
       callback(value);
       
    } else {
        handlers.push(callback);
    }
    return this;
}

executor(resolve, reject);

}

var func = (resolve,reject)=> {
     setTimeout(()=>{
        resolve('Hey I am resolved');
     },3000)
  }

 var basicPromise = new PromisePolyfill(func);
  basicPromise.then( (res) => {
    console.log(res);
   return new PromisePolyfill(func);
   } )
   .then((res)=> {
   console.log(res);
   })

this.then()
必须返回链接到上一个承诺的新承诺。它不应该回报同样的承诺。有几十种从头开始的承诺的示例实现,您可以/应该参考它们,以了解它们在内部是如何工作的。请注意,
.then()
还接受两个参数(第二个参数是可选的),您需要实现
。catch()
此实现还存在许多其他问题。请详细研究规范或研究现有的简单承诺实现。例如,注册时不应同步调用
.then()
处理程序,即使承诺已经实现。您需要
try/catch
来调用所有处理程序吗?您需要支持呼叫处理程序等返回承诺。@jfriend00感谢您指出这个问题。我一定会参考这些示例实现。为什么不直接使用?@Bergi我想了解Promissions的内部结构,所以尝试编写一个polyfill,是的,我确实使用库来实现Promissions:),