Skip to content

Commit f35ac0c

Browse files
authored
Updated promises to async/await
Updated promises to async/await pattern for readability.
1 parent 1acb90d commit f35ac0c

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

docs/spfx/web-parts/guidance/validate-web-part-property-values.md

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -273,35 +273,29 @@ In this step, you validate the provided list name and check if it corresponds to
273273
export default class ListInfoWebPart extends BaseClientSideWebPart<IListInfoWebPartProps> {
274274
// ...
275275
276-
private validateListName(value: string): Promise<string> {
277-
return new Promise<string>((resolve: (validationErrorMessage: string) => void, reject: (error: any) => void): void => {
278-
if (value === null ||
279-
value.length === 0) {
280-
resolve('Provide the list name');
281-
return;
282-
}
283-
284-
this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/web/lists/getByTitle('${escape(value)}')?$select=Id`, SPHttpClient.configurations.v1)
285-
.then((response: SPHttpClientResponse): void => {
286-
if (response.ok) {
287-
resolve('');
288-
return;
289-
}
290-
else if (response.status === 404) {
291-
resolve(`List '${escape(value)}' doesn't exist in the current site`);
292-
return;
293-
}
294-
else {
295-
resolve(`Error: ${response.statusText}. Please try again`);
296-
return;
297-
}
298-
})
299-
.catch((error: any): void => {
300-
resolve(error);
301-
});
302-
});
276+
private async validateListName(value: string): Promise<string> {
277+
if (value === null || value.length === 0) {
278+
return "Provide the list name";
279+
}
280+
281+
try {
282+
let response = await this.context.spHttpClient.get(
283+
this.context.pageContext.web.absoluteUrl +
284+
`/_api/web/lists/getByTitle('${escape(value)}')?$select=Id`,
285+
SPHttpClient.configurations.v1
286+
);
287+
288+
if (response.ok) {
289+
return "";
290+
} else if (response.status === 404) {
291+
return `List '${escape(value)}' doesn't exist in the current site`;
292+
} else {
293+
return `Error: ${response.statusText}. Please try again`;
303294
}
295+
} catch (error) {
296+
return error.message;
304297
}
298+
}
305299
```
306300

307301
First, the `validateListName` method checks if a list name has been provided. If not, it resolves the promise with a relevant validation error. If the user has provided a list name, the `validateListName` method uses the `SPHttpClient` to call the SharePoint REST API and check if the list with the specified name exists.

0 commit comments

Comments
 (0)