Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To pass parameters to a promise function in typescript, create a parameter function and use promise in a function like the below example.

const getNameById = (id: number): Promise<string> => {
  return new Promise(function (resolve, reject) {
    // ...
  })
}

Today, I’m going to show you How do I pass parameters to a promise function in typescript, as above mentioned, I’m going to use the above-mentioned syntax to create and use a promise function with a parameter.

Let’s start today’s tutorial how do you pass parameters to a promise function in typescript?

Typescript promise function with parameter example

Here we will create a parameter function and store it in a variable, in the function we will create a promise with a resolve & reject option where we will check if it is greater than 0 resolve promise or rejects.

const getNameById = (id: number): Promise<string> => {
  return new Promise(function (resolve, reject) {
    if (id > 0) {
      resolve('Stuff worked!');
    } else {
      reject(Error('It broke'));
    }
  })
}

let response: string = await getNameById(3);
console.log(response)
// output: "Stuff worked"

I hope it helps you, All the best đź‘Ť.