The difference between Promise and fetch encapsulating API requests

for example, I need to encapsulate a function that gets the user"s ID

 getUserIds().then((ids) => {console.log(ids)})

// promise
const getUserIds = () => {
   return new Promise(resolve){
   // 
   setTimeout(() => { resolve([1,2,3]) })
}
}
// fetch
const getUserIds = () {
  return fetch(URL, CONFIG)
}

what"s the difference in encapsulating interface functions with promise or fetch?

Feb.27,2021

there is a lot of talk upstairs, and there is no difference.

but there is a problem with the problem itself. Promise has nothing to do with fetch and cannot be compared together. Fetch is a substitute for the original XMLHttpRequest object, which implements the Promise specification and returns Promise instances, while Promise is a set of solutions to solve the problem of asynchronous callbacks.


makes no difference. They all return promise, one is encapsulated by the browser, the other is implemented by themselves


Fetch is based on standard Promise, it is convenient to support async/await
isomorphism, and use isomorphic-fetch

Menu