haterapps / fake-data

Fake Data - A form filler you won't hate
39 stars 2 forks source link

getLastGeneratedValue returns Promise (errors in documentation) #28

Closed fireton closed 1 year ago

fireton commented 1 year ago

getLastGeneratedValue does not returns string but Promise.

So I can't write in my custom generator code something like

return fakeData.getLastGeneratedValue('full_name').toLowerCase().replace(' ', '.') + '@fakemail.com';

instead I have to write

let email = fakeData.getLastGeneratedValue('full_name')
    .then(x => x.toLowerCase().replace(' ', '.') + '@fakemail.com');
return email;
haterapps commented 1 year ago

Hello,

Thank you for pointing it out. I updated the documentation to make it more clear that the return value is a Promise which will resolve to a string (as this is intended).

Your code is correct. Alternatively, you can also write it like this:

return new Promise(async resolve => {
    let full_name = await fakeData.getLastGeneratedValue('full_name');

    resolve(full_name.toLowerCase().replace(' ', '.') + '@fakemail.com');
});
fireton commented 1 year ago

Should I consider always returning Promisein my custom generators code, not strings?

haterapps commented 1 year ago

Pretty much all of the functions that handle generators will return a Promise. But from your custom code, it doesn't matter if you return a string or a Promise. Fake Data will handle both fine.