import { Loading, Notify } from 'quasar' /** * @param {Promise} promise * @param {object} config={notifySuccess, notifyError, useLoading} * @example promiseHandle(new Promise(), { notifyError: { message: 'Erro' }, notifySuccess: { message: 'Sucesso' } }) */ export default async function (promise, config = {}) { const { notifySuccess = {}, notifyError = {}, useLoading = false, onLoading } = config onLoading && onLoading(true) useLoading && Loading.show() const primiseToBeExec = typeof promise === 'function' ? promise() : promise const hasNotify = notify => Object.keys(notify).length try { const data = await (Array.isArray(promise) ? Promise.all(promise) : primiseToBeExec) hasNotify(notifySuccess) && Notify.create({ type: 'success', ...notifySuccess }) return { data, error: null } } catch (error) { hasNotify(notifyError) && Notify.create({ type: 'error', ...notifyError }) return { data: null, error } } finally { onLoading && onLoading(false) Loading.hide() } }
I added a callback onLoading so I can do somenthing while function is running.
onLoading
const { data, error } = await promiseHandler(callbackFn, { onLoading: value => console.log('is loading?', value) })
👨🏼💻
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I added a callback
onLoading
so I can do somenthing while function is running.const { data, error } = await promiseHandler(callbackFn, { onLoading: value => console.log('is loading?', value) })
👨🏼💻