算法八月算法打卡

Posted усил

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法八月算法打卡相关的知识,希望对你有一定的参考价值。

2022-08-01

低配版 promise

class MyPromise 
  constructor(executor) 
    // 成功回调队列
    this._resolveQueue = [];
    // 失败回调队列
    this._rejectQueue = [];

    let resolve = (val) => 
      while (this._resolveQueue.length) 
        const callback = this._resolveQueue.shift();
        callback(val);
      
    

    let reject = (val) => 
      while (this._rejectQueue.length) 
        const callback = this._rejectQueue.shift();
        callback(val);
      
    

    // 创建实例对象时,立即执行 executor 并传入 resolve 和 reject
    executor(resolve, reject);
  

  then = (resolveFunc, rejectFunc) => 
    this._resolveQueue.push(resolveFunc);
    this._rejectQueue.push(rejectFunc);
  


// 测试
const p = new MyPromise((resolve, reject) => 
  setTimeout(() => 
    resolve('result');
  , 2000);
);

p.then(res => console.log(res)); // result
  • Promise 构造方法接收一个 executor(),在 new Promise() 时立即执行该任务。
  • executor() 内部的异步任务会被放入到 宏/微任务队列,等待执行。
  • then()被执行,收集成功/失败回调,放入成功/失败队列。
  • executor()的异步任务被执行,触发resolve/reject,从成功/失败队列中取出回调依次执行。

2022-08-02

Promise A+规范 + then 链式调用

const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";

class MyPromise 
  constructor(executor) 
    this.status = PENDING;
    this.resolveQueue = [];
    this.rejectQueue = [];
    let resolve = (val) => 
      if (this.status !== PENDING) return;

      this.status = FULFILLED;

      while (this.resolveQueue.length) 
        let callback = this.resolveQueue.shift();
        callback(val);
      
    

    let reject = (val) => 
      if (this.status !== PENDING) return;

      this.status = REJECTED;

      while (this.rejectQueue.length) 
        let callback = this.rejectQueue.shift();
        callback(val);
      
    
    executor(resolve, reject);
  
  then = (resolveFunc, rejectFunc) => 
    return new MyPromise((resolve, reject) => 
      let successFunc = (val) => 
        try 
          let x = resolveFunc(val);
          x instanceof MyPromise ? x.then(resolve, reject) : resolve(x);
         catch (error) 
          reject(error);
        
      
      this.resolveQueue.push(successFunc);

      let errorFunc = (error) => 
        try 
          let x = rejectFunc(error);
          x instanceof MyPromise ? x.then(resolve, reject) : reject(x);
         catch (error) 
          reject(error);
        
      
      this.rejectQueue.push(errorFunc);
    )
  

// 测试
const p1 = new MyPromise((resolve, reject) => 
  setTimeout(() => 
    resolve(1)
  , 500);
)

p1.then(res => 
  console.log(res)
  return 2
).then(res => 
    console.log(res)
    return 3
).then(res => 
    console.log(res)
)
//输出 1 2 3

2022-08-03

值穿透、状态变更

// 暂停
const PENDING = 'pending'
// 完成
const FULFILLED = 'fulfilled'
// 拒绝
const REJECTED = 'rejected'
class Mypromise 
    constructor(executor) 
        this.status = PENDING;
        this.preValue = null;
        this.resolveQueue = [];
        this.rejectQueue = [];
        let resolve = (val) => 
            if (this.status !== PENDING) return;
            this.status = FULFILLED;

            this.preValue = val;

            while (this.resolveQueue.length) 
                let callback = this.resolveQueue.shift();
                callback(val);
            
        

        let reject = (val) => 
            if (this.status !== PENDING) return;
            this.status = REJECTED;

            this.preValue = val;

            while (this.rejectQueue.length) 
                let callback = this.rejectQueue.shift();
                callback(val);
            
        
        executor(resolve, reject);
    
    then = (resolveFunc, rejectFunc) => 
        return new MyPromise((resolve, reject) => 

            typeof resolveFunc !== 'function' ? resolveFunc = value => value : null;
            typeof rejectFunc !== 'function' ? rejectFunc = reason => 
                throw new Error(reason instanceof Error ? reason.message : reason)
                 : null;

            let successFunc = (val) => 
                try 
                    let x = resolveFunc(val);
                    x instanceof MyPromise ? x.then(resolve, reject) : resolve(x);
                 catch (error) 
                    reject(error);
                
            
            let errorFunc = (error) => 
                try 
                    let x = rejectFunc(error);
                    x instanceof MyPromise ? x.then(resolve, reject) : reject(x);
                 catch (error) 
                    reject(error);
                
            

            switch (this.status) 
                case PENDING:
                    this.resolveQueue.push(successFunc);
                    this.rejectQueue.push(errorFunc);
                    break;
                case FULFILLED:
                    resolveFunc(this.preValue);
                    break;
                case REJECTED:
                    rejectFunc(this.preValue);
                    break;
            
        )
    


2022-08-04

兼容同步任务

const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";
class MyPromise 
    constructor(executor) 
        this.status = PENDING;
        this.preval = null;
        this.resolveQueue = [];
        this.rejectQueue = [];

        let resolve = (val) => 
           const run = () => 
               if (this.status !== PENDING) return;

               this.status = FULFILLED;
               this.preval = val;

               while (this.resolveQueue.length) 
                   let callback = this.resolveQueue.shift();
                   callback(val);
               
           
           setTimeout(run)
        
        let reject = (val) => 
            const run = () => 
                if (this.status !== PENDING) return;

                this.status = REJECTED;
                this.preval = val;

                while (this.rejectQueue.length) 
                    let callback = this.rejectQueue.shift();
                    callback(val);
                 
            
            setTimeout(run)
        

        executor(resolve, reject);
    
    then = (resolveFunc, rejectFunc) => 
        return new MyPromise((resolve, reject) => 

            typeof resolveFunc !== 'function' ? resolveFunc = val => val : null;
            typeof rejectFunc !== 'function' ? rejectFunc = reason => 
                throw new Error(reason instanceof Error ? reason.message : reason)
             : null;

            let successFunc = (val) => 
                try 
                    let x = resolveFunc(val);
                    x instanceof MyPromise ? x.then(resolve, reject) : resolve(x)
                 catch (error) 
                    reject(error);
                
            

            let errorFunc = (val) => 
                try 
                    let x = rejectFunc(val);
                    x instanceof MyPromise ? x.then(resolve, reject) : reject(x);
                 catch (error) 
                  reject(error);
                
            

            switch (this.status) 
                case PENDING:
                    this.resolveQueue.push(successFunc);
                    this.rejectQueue.push(errorFunc);
                    break;
                case FULFILLED:
                    resolveFunc(this.preval);
                    break;
                case REJECTED:
                    rejectFunc(this.preval);
                    break;
            
        )
    


2022-08-05

promise其他方法

const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

class MyPromise 
    constructor(executor) 
        this.status = PENDING;
        this.val = null;
        this.resolveQueue = [];
        this.rejectQueue = [];

        let resolve = (val) => 
            const run = () => 
                if (this.status !== PENDING) return;

                this.status = FULFILLED;
                this.val = val;

                while (this.resolveQueue.length) 
                    let callback = this.resolveQueue.shift();
                    callback(val);
                
            
            setTimeout(run);
        

        let reject = (val) => 
           const  run = () => 
               if (this.status !== PENDING) return;

               this.status = REJECTED;
               this.val = val;

               while (this.rejectQueue.length) 
                   let callback = this.rejectQueue.shift();
                   callback(val);
               
           
           setTimeout(run);
        
        executor(resolve, reject)
    
    then = (resolveFunc, rejectFunc) => 
        return new MyPromise((resolve, reject) => 
            typeof resolveFunc !== 'function' ? resolveFunc = val => val : null;
            typeof rejectFunc !== 'function' ? rejectFunc = reason => 
                throw new Error(reason instanceof Error ? reason.message : reason);
             : null;

            let successFunc = (val) => 
                try 
                    let x = resolveFunc(val);
                    x instanceof MyPromise ? x.then(resolve, reject) : resolve(x)
                 catch (e) 
                    reject以上是关于算法八月算法打卡的主要内容,如果未能解决你的问题,请参考以下文章

小Y学算法每日LeetCode打卡——15.实现 strStr()

算法打卡,用于自律

小Y学算法⚡️每日LeetCode打卡⚡️——15.实现 strStr()

算法千题案例⚡️每日LeetCode打卡⚡️——61.下一个更大元素 I

OpenMMLab 实战营打卡 - 第 四 课 目标检测算法基础

打卡算法 18四数之和 算法解析