UNPKG

@codelet/core

Version:
49 lines (48 loc) 1.42 kB
import { formatMessage, isFunction, isObject, isString } from '@daysnap/utils'; import { col } from '../codelet'; import { definePlugin } from '../utils'; // 默认配置 const defaultOptions = { formatMessage, excludeMessage: (message) => !message, showToast: (message) => { col.showToast({ title: message, icon: 'none' }); }, }; export const promise = definePlugin((_, options) => { const { excludeMessage, formatMessage, showToast } = Object.assign(defaultOptions, options); Promise.prototype.toast = async function (cb, isThrow) { try { return await this; } catch (err) { const message = formatMessage(err); if (isFunction(cb) && !cb(err, message)) { return; } if (isString(cb)) { showToast?.(cb); } else if (!excludeMessage(message)) { if (isObject(cb)) { ; cb.setData({ error: message }); } if (isThrow) { throw err; } else { showToast?.(message); } } } }; Promise.prototype.null = async function () { try { return await this; } catch (err) { return console.log(err); } }; });