目录

Created by gh-md-toc

utils

JavaScript中常用的一些函数

说明

此README文档通过执行npm run readme命令生成,目录是通过gh-md-toc生成。

使用方法

Examples

promise-allSettled.js

promise-allSettled.js 实现Promise.allSettled函数,类似Promise.all(),但是结果同时包含resolve和reject。
对于每个结果对象,都有一个status 字符串。如果状态为fulfilled,则存在一个value 。如果状态为rejected,则说明原因 。

const delay = time => 
  new Promise((resolve, reject) => {
    setTimeout(()=>{resolve(time);}, time)
  })

promiseAllSettled([
  Promise.reject(1),
  delay(300),
  delay(100),
  Promise.resolve(4)
]).then( res => {
  console.log(res) // => 1, 300, 100, 4
});

promiseAllSettled([Promise.reject(1), Promise.resolve(2)]).then( res => {
  console.log(res)
})

promise-any.js

promise-any.js 实现Promise.any函数,类似Promise.all(),但是会忽略掉reject,所以它只需要有resolve的即可。

const delay = time => 
  new Promise((resolve, reject) => {
    setTimeout(()=>{resolve(time);}, time)
  })

promiseAny([
  Promise.reject(1),
  delay(300),
  delay(100),
  Promise.resolve(4)
]).then( res => {
  console.log('one resolved',res) // => [4, 100, 300]
});

promiseAny([Promise.reject(1), Promise.reject(2)]).then( res => {
  console.log('one resolved')
}).catch( err => {
  console.log('all rejected') // => all rejected
});

promise-delay.js

promise-delay.js promise封装setTimeout延时处理

const pdObj = promiseDelay(1000);
pdObj.promise().then(()=>{
  console.log('done')
});

// 取消setTimeout
pdObj.clearTimeout();

promise-first.js

promise-first.js 实现Promise.first函数,返回最先resolve的promise结果

说明:实现思路:拒绝promiseNone则表示至少有一个promise是resolve状态,我个人认为这个叫做Promise.one或Promise.some更合适

const delay = time => 
  new Promise((resolve, reject) => {
    setTimeout(()=>{resolve(time);}, time)
  })

promiseFirst([
  Promise.reject(1),
  delay(300),
  delay(100),
  Promise.resolve(4)
]).then( res => {
  console.log('one resolved', res) // => 4, not 300, not [4,100,300]
});

promiseFirst([Promise.reject(1), Promise.reject(2)]).then( res => {
  console.log('one resolved')
}).catch( err => {
  console.log('all rejected') // => all rejected
});

promise-last.js

promise-last.js 实现Promise.last函数,返回最后一个resolve的promise结果

说明:实现思路:promiseAny()的最后一个则为last

const delay = time => 
  new Promise((resolve, reject) => {
    setTimeout(()=>{resolve(time);}, time)
  })

promiseLast([
  Promise.reject(1),
  delay(300),
  delay(100),
  Promise.resolve(4)
]).then( res => {
  console.log('one resolved', res) // => 300, not 4
});

promiseLast([Promise.reject(1), Promise.reject(2)]).then( res => {
  console.log('one resolved')
}).catch( err => {
  console.log('all rejected') // => all rejected
});

promise-map.js

promise-map.js 实现Promise.map函数,对所有promise迭代执行某操作

promiseMap([Promise.resolve(1), Promise.reject("Oops")], function resolveFn(v){
  return v * 2
}, function rejectFn(v){
  return `err: ${v}`
}).then( res => {
  console.log(res) // => [2, "err: Oops"]
});

promise-none.js

promise-none.js 实现Promise.none函数,所有promise被拒绝则表示完成

promiseNone([Promise.reject(1), Promise.reject(2)]).then( res => {
  console.log('all rejected')
});

promiseNone([Promise.resolve(1), Promise.reject(2)]).then( res => {
  console.log('all rejected')
}).catch( err => {
  console.log('one was not rejected')
});

promise-timeout.js

promise-timeout.js 简单的超时处理

const taskPromise = new Promise( (resolve, reject) => {
  // 任意异步事件
  var time = Math.random() * 2000;
  setTimeout(function(){
    resolve('done');
  }, time);
});
const a = promiseTimeout(taskPromise, 1000).then(() => {
  console.log('done')
}).catch((err) => {
  console.log(err) // err为promiseTimeout抛出的错误
})

promisify.js

promisify.js 把非promise的回调函数A转成promise调用的函数B
A必须遵循:1. 回调函数在主函数中的参数位置必须是最后一个;2. 回调函数参数中的第一个参数必须是 error 。

// 原有的callback调用
fs.readFile('test.js', function(err, data) {
  if (!err) {
    console.log(data);
  } else {
    console.log(err);
  }
});

// promisify后
var readFileAsync = promisify(fs.readFile);
readFileAsync('test.js').then(data => {
  console.log(data);
}, err => {
  console.log(err);
});

quick-sort.js

quick-sort.js 快排算法

const a = [2,13,3,14,5]
const b = quickSort(a)
console.log(b) // => [2,3,5,13,14]

shuffle.js

shuffle.js 数组随机排序(扑克牌算法)

const a = [1,2,3,4,5]
shuffle(a)
console.log(a) // => [3, 1, 5, 2, 4]

stable-sort.js

stable-sort.js 稳定排序(Array.prototype.sort是非稳定排序)

const a = [2,1,3,4,3]
const b = stableSort(a)
console.log(b) // => [1,2,3,3,4]

许可证

MIT