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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@js-rocks/promise-tiny

v0.1.0

Published

simple promise implementation

Readme

Promise A+ TDD implementation

Resources

Implementation details

Producer && consumer

  • producer: resolve, reject

  • consumer: then, catch, finally

  • all

  • race

  • Promise.resolve, promise.reject

Macro/Micro task

The built-in Promise in the browser uses micro-task, we can only use macro-ask simulation, here we use setTimeout

resolve delayed binding

Resolve must be executed in the next event cycle, otherwise the onFulfilled and onRejected functions in then have not been bound, and if the following code does not add setTimeout, an error will be reported

// app
let promise = new MPromise(resolve => {
  resolve('a-ok')
})
promise.then(promiseSpy)


resolve(value) {
  // setTimeout(() => {
    this.state = 1
    this.value = value
    this.onFulfilled(value)
  // })
}

The binding of this

Resolve and reject will be executed in the executor scope, and this needs to be bound to the current instance

Promise chain

The then line of each promise becomes the starting point of a single promise chain

let promise = new MPromise(resolve => {
  resolve(42);
});
promise.then(firstSpy); // start point of chain1
promise.then(secondSpy); // start point of chain2
promise
  .then(firstSpy)
  .then('..')
  .then('..'); // the continuous extension of the chain

Then returns another promise to form a promise chain, and at the same time, the current promise queue needs to save the pointer of the next promise

then(onFulfilled = () => {}, onRejected) {
  let promise = new MPromise(() => {})
  // save the pointer of the next promise
  this.pending.push([promise, onFulfilled, onRejected])
  if (this.state === 1) {
    this.scheduleQueue(this.value)
  }
  return promise
}

In this way, the resolve of the next promise is called when the current promise resolves, forming a chain reaction

while (this.pending.length) {
  let [promise, onFulfilled, onRejected] = this.pending.shift();
  // 触发链条上的下一个promise
  promise.resolve(this.value);
}

Promise returns promise

The key point is where in the chain this promise is inserted

The original thought was to process it in the scheduleQueue function

if (isFunction(onFulfilled)) {
let returnResult = onFulfilled(this.value)
if (isFunction(returnResult instanceof MPromise)) {
  // 当前promise作为consumer, returnResult作为producer
  return promise.then(
    () => {
      return returnResult.resolve(returnResult.value)
    },
    () => {
      return returnResult.reject(returnResult.value)
    }
  )
} else {
  promise.resolve(returnResult)
}

Finally found that it can only be processed in the resolve function.

When the value is returned, the resolve of the value(promise) will be called at some point in the future.

At this point, our current promise has been suspended.

When the resolve of value is called, the value will be passed to the resolve of promise, and the promise continues.

resolve(value) {
  if (value && isFunction(value.then)) {
    value.then(this.resolve.bind(this), this.reject.bind(this))
  } else {
    this.state = 1
    this.value = value
    this.scheduleQueue()
  }
}