Skip to content

[js]使一个返回Promise的函数同时支持Error-first回调形式与Promise调用方式。 #25

@Jiasm

Description

@Jiasm

使函数同时支持两种调用方法:

function pro(a, b) {
  return new Promise((resolve, reject) => {
    resolve(a + b)
  })
}

function wrap(func) {
  // code here
}

let func = wrap(pro)

func(1, 2, (err, result) => {
  if (err) {
    console.error(err)
  } else {
    console.log(result)
  }
})

func(1, 2).then(console.log, console.error)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions