Call getSmiles() and getSmarts() Chained

Q

How to call getSmiles() and getSmarts() methods in 2 promises one chained to the other?

✍: FYIcenter.com

A

Another way to avoid the bug mentioned in the previous tutorial is to call ketcher.getSmiles() and ketcher.getSmarts() methods in 2 chained promises.

Chained promises use fulfilled and rejected handlers of the previous promise to return the next promise.

The following HTML document showdraganddropsymbolindepictmode you how to call ketcher.getSmiles() and ketcher.getSmarts() in chained promises.

<html>
<!-- export-as-SMILES-SMARTS-chained.html Copyright (c) FYIcenter.com. -->
<head>
<title>Export Chemical Structure in SMILES and SMARTS Formats</title>
</head>
<body>
<p>Create a chemical structure, then click export buttons below the editor:</p>
<iframe id=frmKetcher src="/ketcher/index.html"
  style="width: 680px; height: 400px;"></iframe>

<p><button onclick="exportBoth();">Export in SMILES and SMARTS in chained promises</button></p>
<p>SMILES:</p>
<pre id=smiles style="background-color: #ddd;"></pre>
<p>SMARTS:</p>
<pre id=smarts style="background-color: #ddd;"></pre>
<script>
  var ketcher = null;

function exportBoth() {
  frm = document.getElementById("frmKetcher");
  ketcher = frm.contentWindow.ketcher;

  promiseSmiles = ketcher.getSmiles();
  promiseSmarts = promiseSmiles.then(consumeSmiles, consumeSmiles);
  promiseSmarts.then(consumeSmarts, consumeSmarts);
}

function consumeSmiles(result) {
  document.getElementById("smiles").innerHTML = result.toString();
  return ketcher.getSmarts(); 
}

function consumeSmarts(result) {
  document.getElementById("smarts").innerHTML = result.toString();
}
</script>
</body>
</html>

Open the above HTML document on your local Web server. Draw a simple molecule structure in the editor.

Click "Export in SMILES and SMARTS in chained promises" button, you will see that both promises work correctly with SMILES and SMARTS strings returned to the page.

 

Export Chemical Structure in All Formats

Call getSmiles() and getSmarts() Nested

Ketcher JavaScript API

⇑⇑ Ketcher - Chemical Structure Editor

2024-01-15, 346🔥, 0💬