温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/bun/%24/ShellPromise
点击访问原文链接

class ShellPromise | Bun module | Bun

class ShellPromise | Bun module | BunBuildDocsReferenceGuidesBlogDiscord/Bun/$/ShellPromiseP[Symbol.species]P[Symbol.toStringTag]MallMallSettledManyMarrayBufferMblobMcatchCoconstructorMcwdMenvMfinallyMjsonMlinesMnothrowMquietMraceMrejectMresolveAcstdinMtextMthenMthrowsMtryMwithResolvers

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/Bun/$/ShellPromiseP[Symbol.species]P[Symbol.toStringTag]MallMallSettledManyMarrayBufferMblobMcatchCoconstructorMcwdMenvMfinallyMjsonMlinesMnothrowMquietMraceMrejectMresolveAcstdinMtextMthenMthrowsMtryMwithResolvers

class

$.ShellPromiseclass ShellPromise

The Bun.$.ShellPromise class represents a shell command that gets executed once awaited, or called with .text(), .json(), etc.

const myShellPromise = $`echo "Hello, world!"`;
const result = await myShellPromise.text();
console.log(result); // "Hello, world!"
readonly [Symbol.toStringTag]: stringreadonly static [Symbol.species]: PromiseConstructorarrayBuffer(): PromiseArrayBuffer>;

Read from stdout as an ArrayBuffer

Automatically calls quiet

@returns

A promise that resolves with stdout as an ArrayBuffer

const output = await $`echo hello`.arrayBuffer();
console.log(output); // ArrayBuffer { byteLength: 6 }
blob(): PromiseBlob>;

Read from stdout as a Blob

Automatically calls quiet

@returns

A promise that resolves with stdout as a Blob

const output = await $`echo hello`.blob();
console.log(output); // Blob { size: 6, type: "" }
catchTResult = never>(onrejected?: null | (reason: any) => TResult | PromiseLikeTResult>): PromiseShellOutput | TResult>;

Attaches a callback for only the rejection of the Promise.

@param onrejected

The callback to execute when the Promise is rejected.

@returns

A Promise for the completion of the callback.

cwd(newCwd: string): this;

Change the current working directory of the shell.

@param newCwd

The new working directory

env(newEnv: undefined | Dictstring> | Recordstring, undefined | string>): this;

Set environment variables for the shell.

@param newEnv

The new environment variables

await $`echo $FOO`.env({ ...process.env, FOO: "LOL!" })
expect(stdout.toString()).toBe("LOL!");
finally(onfinally?: null | () => void): PromiseShellOutput>;

Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

@param onfinally

The callback to execute when the Promise is settled (fulfilled or rejected).

@returns

A Promise for the completion of the callback.

json(): Promiseany>;

Read from stdout as a JSON object

Automatically calls quiet

@returns

A promise that resolves with stdout as a JSON object

const output = await $`echo '{"hello": 123}'`.json();
console.log(output); // { hello: 123 }
lines(): AsyncIterablestring>;

Read from stdout as a string, line by line

Automatically calls quiet to disable echoing to stdout.

nothrow(): this;

Configure the shell to not throw an exception on non-zero exit codes. Throwing can be re-enabled with .throws(true).

By default, the shell with throw an exception on commands which return non-zero exit codes.

quiet(isQuiet?: boolean): this;

By default, the shell will write to the current process's stdout and stderr, as well as buffering that output.

This configures the shell to only buffer the output.

@param isQuiet

Whether to suppress output. Defaults to true.

text(encoding?: BufferEncoding): Promisestring>;

Read from stdout as a string.

Automatically calls quiet to disable echoing to stdout.

@param encoding

The encoding to use when decoding the output

@returns

A promise that resolves with stdout as a string

Read as UTF-8 string

const output = await $`echo hello`.text();
console.log(output); // "hello\n"

Read as base64 string

const output = await $`echo ${atob("hello")}`.text("base64");
console.log(output); // "hello\n"
thenTResult1 = ShellOutput, TResult2 = never>(onfulfilled?: null | (value: ShellOutput) => TResult1 | PromiseLikeTResult1>,onrejected?: null | (reason: any) => TResult2 | PromiseLikeTResult2>): PromiseTResult1 | TResult2>;

Attaches callbacks for the resolution and/or rejection of the Promise.

@param onfulfilled

The callback to execute when the Promise is resolved.

@param onrejected

The callback to execute when the Promise is rejected.

@returns

A Promise for the completion of which ever callback is executed.

throws(shouldThrow: boolean): this;

Configure whether or not the shell should throw an exception on non-zero exit codes.

By default, this is configured to true.

static allT extends [] | readonly unknown[]>(values: T): Promise{ [K in string | number | symbol]: AwaitedT[PP>]> }>;

Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

@param values

An array of Promises.

@returns

A new Promise.

static allSettledT>(values: IterableT | PromiseLikeT>>): PromisePromiseSettledResultAwaitedT>>[]>;

Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.

@param values

An array of Promises.

@returns

A new Promise.

static anyT extends [] | readonly unknown[]>(values: T): PromiseAwaitedT[number]>>;

The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

@param values

An array or iterable of Promises.

@returns

A new Promise.

static raceT extends [] | readonly unknown[]>(values: T): PromiseAwaitedT[number]>>;

Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

@param values

An array of Promises.

@returns

A new Promise.

static rejectT = never>(reason?: any): PromiseT>;

Creates a new rejected promise for the provided reason.

@param reason

The reason the promise was rejected.

@returns

A new rejected Promise.

static resolveT>(value: T): PromiseAwaitedT>>;

Creates a new resolved promise for the provided value.

@param value

A promise.

@returns

A promise whose internal state matches the provided promise.

static tryT, A extends any[] = []>(fn: (...args: A) => T | PromiseLikeT>,...args: A): PromiseT>;

Try to run a function and return the result. If the function throws, return the result of the catch function.

@param fn

The function to run

@param args

The arguments to pass to the function. This is similar to setTimeout and avoids the extra closure.

@returns

The result of the function or the result of the catch function

static withResolversT>(): { promise: PromiseT>; reject: (reason?: any) => void; resolve: (value?: T | PromiseLikeT>) => void };

Create a deferred promise, with exposed resolve and reject methods which can be called separately.

This is useful when you want to return a Promise and have code outside the Promise resolve or reject it.

const { promise, resolve, reject } = Promise.withResolvers();

setTimeout(() => {
resolve("Hello world!");
}, 1000);

await promise; // "Hello world!"

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

class ShellPromise | Bun module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    The `Bun.$.ShellPromise` class represents a shell command that gets executed once awaited, or called with `.text()`, `.json()`, etc.