npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@singcl/promise

v0.2.1

Published

A promise Promise/A+

Downloads

17

Readme

promise

Promise/A+

Travis npm (scoped) code style: prettier David David Github file size npm

badge: https://img.shields.io/

USAGE

  1. install: npm i @singcl/promise
  2. In project:
var Promise = require('@singcl/promise')

var promise = new Promise(function(resolve, reject) {
	resolve('This is an example')
})

promise.then(function(v) {
	console.log(v)
})

// This is an example

Promise标准简读

1. 只有一个then方法,没有race,all等方法,甚至没有构造函数

Promise标准中仅指定了Promise对象的then方法的行为,其它一切我们常见的方法/函数都并没有指定,包括catch,race,all等常用方法,甚至也没有指定该如何构造出一个Promise对象。另外then也没有一般实现中(Q, $q等)所支持的第三个参数,一般称onProgress.

2. then方法返回一个新的Promise

Promise的then方法返回一个新的Promise,而不是返回this

// 假设已经实现了一个Promise对象promise1
var promise2 = promise1.then(/*your code*/)
console.log(promise2 === promise1)  // fales

3. 不同Promise的实现需要可以相互调用

4. 三个状态:pending,resolved,rejected

Promise的初始状态为pending,它可以由此状态转换为fulfilled(resolved)或者rejected,Promise的状态一旦从pending变为fulfilled或者rejected就永远不会在变化.

5. 更多规范参照Promise官方英文文档

Promise实现

  • [x] Promise构造函数基本结构
  • [x] Promise原型方法:then
  • [x] Promise原型方法:catch

问题探讨

then方法的callbakc为什么要设计成异步调用?

答:Promise 的机制就是 then 回调函数必须异步执行。为什么?因为这样保障了代码执行顺序的一致性。

先看一个场景:

promise.then(function() { 
  if (trueOrFalse) { 
    // 同步执行 
    foo(); 
  } else { 
    // 异步执行 (如:使用第三方库)
    setTimeout(function() { 
        foo(); 
    }) 
  } 
}); 

bar();

如果 promise then 回调是同步执行的,请问 foo() 和 bar() 函数谁先执行?

答案是,如果 trueOrFalse 为 true 则 foo() 先执行,bar() 后执行;否则 bar() 先执行,foo() 后执行。在大部分情况下,你没法预料到 trueOrFalse 的值,这也就意味着,你不能确定这段代码真正的执行顺序,这可能会导致一些难以想到的 bug。

如果 promise then 回调是异步执行的,请问 foo() 和 bar() 函数谁先执行? 答案一目了然,bar() 先执行,foo() 后执行。

所以为了保证代码执行顺序的一致性, then 回调必须保证是异步的。

除此之外,Promise 的设计还是为了解决以下情形导致的不确定性:

  • Call the callback too early
  • Call the callback too late (or never)
  • Call the callback too few or too many times
  • Fail to pass along any necessary environment/parameters
  • Swallow any errors/exceptions that may happen

更多关于该问题的探讨可以阅读:https://www.zhihu.com/question/57071244

License FOSSA Status

FOSSA Status