@/utils#isPromise TypeScript Examples
The following examples show how to use
@/utils#isPromise.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCFunctionClass.ts From sandstone with MIT License | 4 votes |
/**
* Call the actual function overload. Returns different informations about it.
*/
generate = (): R => {
const { commandsRoot } = this.datapack
if (this.alreadyInitialized) {
return undefined as any
}
// Get the given tags
let tags = this.options.tags ?? []
// If it should run each tick, add it to the tick.json function
const opts = this.options as any
const runEveryDelay = opts.runEvery ?? opts.runEach
if (runEveryDelay !== undefined) {
if (typeof runEveryDelay === 'number' && runEveryDelay < 0) { throw new Error(`\`runEvery\` argument must be greater than 0, got ${runEveryDelay}`) }
if (this.options.runOnLoad !== false) {
// If run on load, call it directly
tags = [...tags, 'minecraft:load']
}
} else {
// If runEachTick is specified, add to minecraft:tick
if (opts.runEveryTick ?? opts.runEachTick) {
tags = [...tags, 'minecraft:tick']
}
// Idem for load
if (this.options.runOnLoad) {
tags = [...tags, 'minecraft:load']
}
}
const warning = chalk.hex('#FFA500')
if (opts.runEachTick !== undefined) {
console.warn(warning('The `runEachTick` MCFunction option is deprecated, use `runEveryTick` instead.'))
}
if (opts.runEach !== undefined) {
console.warn(warning('The `runEach` MCFunction option is deprecated, use `runEvery` instead.'))
}
for (const tag of tags) {
if (typeof tag === 'string') {
this.datapack.addFunctionToTag(this.name, tag)
} else {
tag.values.push(this.name)
}
}
const previousFunction = this.datapack.currentFunction
// Choose the conflict strategy
let onConflict: ResourceConflictStrategy<'functions'>
let previousResource: FunctionResource & { isResource: true }
if (this.onConflict === 'append' || this.onConflict === 'prepend') {
onConflict = (oldFunc, newFunc) => {
previousResource = oldFunc
newFunc.children = oldFunc.children
return newFunc
}
} else {
onConflict = this.onConflict
}
const resource = this.generateResource(onConflict)
this.datapack.currentFunction = resource
const result = this.callback()
// If the command was scheduled to run each n ticks, add the /schedule command
if (runEveryDelay) {
this.datapack.commandsRoot.schedule.function(this.name, runEveryDelay, 'append')
}
const afterCall = () => {
// If there is an unfinished command, register it
commandsRoot.register(true)
if (this.onConflict === 'append') {
resource.commands = [...(previousResource?.commands ?? []), ...resource.commands]
}
if (this.onConflict === 'prepend' && previousResource) {
resource.commands = [...resource.commands, ...(previousResource?.commands ?? [])]
}
// Then back to the previous one
this.datapack.currentFunction = previousFunction
}
this.alreadyInitialized = true
if (isPromise(result)) {
return result.then(afterCall) as any
}
return afterCall() as any
}